#include "PointHandler.h"
#include "SendPointHandlerBase.h"

PointHandler::PointHandler()
{
}

PointHandler::~PointHandler()
{
}

void PointHandler::input(const QVariant& data, const QString& rawInput, CommandSource cs)
{
	if (cs == CS_MOUSEMOVE)
		return;
//	auto sph = getSendPointHandler();

	if (status() == 0)
	{
		prompt(1, m_prompt0);
		return;
	}
	//xyz
	if (status() == 1)
	{
		if (data.type() == QVariant::Vector3D)
		{
			auto pt = data.value<QVector3D>();
			m_result.set(pt.x(), pt.y(), pt.z());
			m_done = true;
		}
		else if (data.type() == QVariant::PointF)
		{
			auto pt = data.value<QPointF>();
			m_result.set(pt.x(), pt.y(), 0);
			prompt(2, tr("输入z分量:"));
		}
		else if (data.type() == QVariant::Double)
		{
			double x = data.value<double>();
			m_result.set(x, 0, 0);
			prompt(3, tr("输入yz分量或者[y]:"));
		}
		else if (data.type() == QVariant::String)
		{
			const auto input = data.toString();
			for (const auto& outerOption : m_outerOptions)
			{
				if (input == outerOption)
				{
					m_done = true;
					m_isResultPoint = false;
					m_resultInput = input;
					return;
				}
			}
			if (input == "x")
			{
				prompt(5, tr("输入x分量:"));
			}
			else if (input == "xy")
			{
				prompt(6, tr("输入xy分量:"));
			}
			else if (input == "xz")
			{
				prompt(7, tr("输入xz分量:"));
			}
		}
		return;
	}
	//z
	if (status() == 2)
	{
		if (data.type() == QVariant::Vector3D)
		{
			auto pt = data.value<QVector3D>();
			m_result.set(m_result.x(), m_result.y(), pt.z());
			m_done = true;
		}
		else if (data.type() == QVariant::Double)
		{
			double z = data.value<double>();
			m_result.set(m_result.x(), m_result.y(), z);
			m_done = true;
		}
		return;
	}
	//yz
	if (status() == 3)
	{
		if (data.type() == QVariant::Vector3D)
		{
			auto pt = data.value<QVector3D>();
			m_result.set(m_result.x(), pt.y(), pt.z());
			m_done = true;
		}
		else if (data.type() == QVariant::PointF)
		{
			auto pt = data.value<QPointF>();
			m_result.set(m_result.x(), pt.x(), pt.y());
			m_done = true;
		}
		else if (data.type() == QVariant::String)
		{
			if (data.toString() == "y")
			{
				prompt(4, tr("输入y分量或者[yz]:"));
			}
		}
		return;
	}
	//y
	if (status() == 4)
	{
		if (data.type() == QVariant::Vector3D)
		{
			auto pt = data.value<QVector3D>();
			m_result.set(m_result.x(), pt.y(), 0);
			prompt(2, tr("输入z分量:"));
		}
		else if (data.type() == QVariant::Double)
		{
			double y = data.value<double>();
			m_result.set(m_result.x(), y, 0);
			prompt(2, tr("输入z分量:"));
		}
		return;
	}
	//x
	if (status() == 5)
	{
		if (data.type() == QVariant::Vector3D)
		{
			auto pt = data.value<QVector3D>();
			m_result.set(pt.x(), 0, 0);
			prompt(3, tr("输入yz分量或者[y]:"));
		}
		else if (data.type() == QVariant::Double)
		{
			double x = data.value<double>();
			m_result.set(x, 0, 0);
			prompt(3, tr("输入yz分量或者[y]:"));
		}
		return;
	}
	//xy
	if (status() == 6)
	{
		if (data.type() == QVariant::Vector3D)
		{
			auto pt = data.value<QVector3D>();
			m_result.set(pt.x(), pt.y(), 0);
			prompt(2, tr("输入z分量:"));
		}
		else if (data.type() == QVariant::PointF)
		{
			auto pt = data.value<QPointF>();
			m_result.set(pt.x(), pt.y(), 0);
			prompt(2, tr("输入z分量:"));
		}
		return;
	}
	//xz
	if (status() == 7)
	{
		if (data.type() == QVariant::Vector3D)
		{
			auto pt = data.value<QVector3D>();
			m_result.set(pt.x(), 0, pt.z());
			prompt(8, tr("输入y分量:"));
		}
		else if (data.type() == QVariant::PointF)
		{
			auto pt = data.value<QPointF>();
			m_result.set(pt.x(), 0, pt.y());
			prompt(8, tr("输入y分量:"));
		}
		return;
	}
	//y
	if (status() == 8)
	{
		if (data.type() == QVariant::Vector3D)
		{
			auto pt = data.value<QVector3D>();
			m_result.set(m_result.x(), pt.y(), m_result.z());
			m_done = true;
		}
		else if (data.type() == QVariant::Double)
		{
			double y = data.value<double>();
			m_result.set(m_result.x(), y, m_result.z());
			m_done = true;
		}
		return;
	}
}

bool PointHandler::done() const
{
	return m_done;
}

void PointHandler::setPrompt0(const QString & leftPrompt, const QStringList & optionPrompts, const QStringList & optionInputs, QString defaultOption)
{
	m_outerOptions = optionInputs;
	m_prompt0 = leftPrompt;
	assert(optionPrompts.size() == optionInputs.size());
	m_prompt0 += "[x/xy/xz";
	for(int i=0; i< optionPrompts.size(); ++i)
	{
		m_prompt0 += "/";
		m_prompt0 += optionPrompts[i];
		if (!optionInputs[i].isEmpty())
		{
			m_prompt0 += "(";
			m_prompt0 += optionInputs[i];
			m_prompt0 += ")";
		}
	}
	m_prompt0 += "]";
	if (!defaultOption.isEmpty())
		m_prompt0 += "<" + defaultOption + ">";
	m_prompt0 += ": ";
}

bool PointHandler::isResultPoint() const
{
	return m_isResultPoint;
}

osg::Vec3d PointHandler::getPoint() const
{
	return m_result;
}

QString PointHandler::getResultInput() const
{
	return m_resultInput;
}

void PointHandler::reset()
{
	setStatus(0);
	m_done = false;
	input(QVariant(), "", CS_CMDLINE);
}
